import GroupUtil from "@/GroupUtil";

export default class 选取 {
  static 范围选取友军(
    中心: unit,
    半径: number,
    x = GetUnitX(中心),
    y = GetUnitY(中心)
  ) {
    let group = GroupUtil.groupObjectPool.borrowObject();
    GroupEnumUnitsInRange(group, x, y, 半径, null);
    let units: unit[] = [];
    let myPlayer = GetOwningPlayer(中心);
    for (let i = 0; i <= 1000000; i++) {
      let unitHandle = FirstOfGroup(group);
      if (!IsHandle(unitHandle)) {
        break;
      }
      GroupRemoveUnit(group, unitHandle);
      if (GetUnitState(unitHandle, UNIT_STATE_LIFE) < 1) {
        continue;
      }
      if (IsUnitAlly(unitHandle, myPlayer)) {
        units.push(unitHandle);
      }
    }
    GroupUtil.groupObjectPool.returnObject(group);
    return units;
  }

  static 范围选取友军_排除中心单位(中心: unit, 半径: number) {
    let all = 选取.范围选取友军(中心, 半径, GetUnitX(中心), GetUnitY(中心));
    for (let item of all) {
      if (item == 中心) {
        all.splice(all.indexOf(item), 1);
        break;
      }
    }
    return all;
  }
}
